package com.central.game.service.impl;

import com.central.annotation.datasource.DataSource;
import com.central.game.dao.ResourceDao;
import com.central.game.model.ResourceInfo;
import com.central.game.service.ResourceService;
import com.central.model.common.PageResult;
import com.central.model.common.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ResourceServiceImpl implements ResourceService {


    @Autowired
    private ResourceDao resourceDao;


    @DataSource(name = "log")
    @Override
    public PageResult<ResourceInfo> findResource(Map<String, Object> params) {
        log.info("资源查找");
        PageUtil.pageParamConver(params,true);
        int total = resourceDao.resourceCount(params);
        List<ResourceInfo> list = Collections.emptyList();
        if (total > 0){
            list = resourceDao.findResourceList(params);
        }
        return PageResult.<ResourceInfo>builder().data(list).code(0).count((long)total).build();
    }

    @DataSource(name = "log")
    @Override
    public void resourceUpdate(ResourceInfo resourceInfo) {
        log.info("资源更新");
        resourceDao.updateResource(resourceInfo);
    }

    @DataSource(name = "log")
    @Override
    public void resourceSave(ResourceInfo resourceInfo) {
        log.info("新增资源");
        try{
            resourceDao.saveResource(resourceInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @DataSource(name = "log")
    @Override
    public void resourceDel(Long id) {
        log.info("资源删除:" + id);
        resourceDao.delResource(id);
    }
}
